import { requestGoodinfo, requestShopChart } from '@/api'
import { getUUId } from '@/utils/uuid_token'

export default {
  namespaced: true,
  actions: {
    async getGoodInfo(context, val) {
      let result = await requestGoodinfo(val)
      if (result.code === 200) {
        context.commit('GETGOODINFO', result.data)
      }
    },
    async getShopChart(context, { skuId, skuNum }) {
      // await解析promise里面的内容
      // async让内容变成一个promise
      let result = await requestShopChart(skuId, skuNum)
      if (result.code === 200) {
        return 'OK！'
      } else {
        Promise.reject(new Error('failed'))
      }
    }
  },
  mutations: {
    GETGOODINFO(state, val) {
      state.goodInfo = val
    }
  },
  state: {
    goodInfo: {},
    // 游客临时身份
    uuid_token: getUUId()
  },
  getters: {
    // 请求数据可能还未完全回来，所以要判断是否为空，空则赋{},否则报错
    categoryView(state) {
      return state.goodInfo.categoryView || {}
    },
    skuInfo(state) {
      return state.goodInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
      return state.goodInfo.spuSaleAttrList || []
    },
  }
}